package main

import (
	"log"
	"math/rand"
	"os"
	"server5/Dao"
	"server5/Routers"
	"server5/protobuf"
	"server5/server"
	"server5/utils"
	"time"
)

//单例模式在使用之前先初始化，避免线程安全问题
func init() {
	utils.GetGlobalObject()
	server.GetMsgManager()
	server.GetConnManager()
	server.GetGameLobby()
	utils.GetRedisConnPool("localhost:6379", 16, 0, 300*time.Second)
	Dao.GetUserDao()
	//随机种子
	rand.Seed(time.Now().UnixNano())
	server.GetMap().ReadMap()
	logFile, err := os.OpenFile("./log.log",os.O_WRONLY|os.O_CREATE|os.O_APPEND,0666)
	if err != nil {
		os.Exit(0)
	}
	log.SetOutput(logFile)
}

func main() {
	s := server.NewServer()
	//添加路由
	s.AddRouter(protobuf.MsgType_Login, Routers.Login{})
	s.AddRouter(protobuf.MsgType_Register,Routers.Register{})
	s.AddRouter(protobuf.MsgType_StartGame,Routers.StartGame{})
	s.AddRouter(protobuf.MsgType_CancelStartGame,Routers.CancelStartGame{})
	s.AddRouter(protobuf.MsgType_BuyUnit,Routers.BuyUnit{})
	s.AddRouter(protobuf.MsgType_MoveUnit,Routers.MoveUnit{})
	s.AddRouter(protobuf.MsgType_AttackUnit,Routers.AttackUnit{})
	s.AddRouter(protobuf.MsgType_UpStronghold,Routers.UpStronghold{})
	s.AddRouter(protobuf.MsgType_OccupyStronghold,Routers.OccupyStronghold{})
	s.AddRouter(protobuf.MsgType_CancelOccupyStronghold,Routers.CancelOccupyStronghold{})
	s.AddRouter(protobuf.MsgType_SkillInfo,Routers.SKillInfo{})
	s.AddRouter(protobuf.MsgType_UpdateLand,Routers.UpdateLand{})
	s.Start()
}
